package com.ruicar.afs.cloud.basic.costexempt.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.basic.common.entity.BasicExpenseRecordDetail;
import com.ruicar.afs.cloud.basic.common.service.BasicExpenseRecordDetailService;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.basic.costexempt.common.Constants;
import com.ruicar.afs.cloud.basic.costexempt.condition.BasicCostExemptCondition;
import com.ruicar.afs.cloud.basic.costexempt.vo.BasicExpenseRecordDetailVo;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitRecordService;
import com.ruicar.afs.cloud.batch.service.AfsBatchInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ExpenseStatusEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.FeeTypeEnum;
import com.ruicar.afs.cloud.manage.api.dto.BasicExpenseRecordDetailDto;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: sang jian
 * @date: 2020/8/5 16:24
 * @description:
 */

@Slf4j
@RestController
@RequestMapping("/costExempt")
@AllArgsConstructor
public class BasicCostExemptController {
    private final BasicMainInfoService basicMainInfoService;
    private final BasicExpenseRecordDetailService basicExpenseRecordDetailService;
    private final BasicDebitRecordService basicDebitRecordService;
    private final AfsBatchInfoService afsBatchInfoService;

    public Date getBatchDate(){
        return DateUtil.parse(afsBatchInfoService.getSystemBatchInfo("icos").getBatchDate(), DatePattern.PURE_DATE_PATTERN);
    }

    @PostMapping("/getCanCostExemptListByCondition")
    @ApiOperation(value = "多条件分页获取可以做豁免费用列表")
    public IResponse getCanCostExemptListByCondition(@RequestBody BasicCostExemptCondition condition) {
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        //先查出来可以做豁免费用的列表
        List<BasicExpenseRecordDetail> list = basicExpenseRecordDetailService.list(Wrappers.<BasicExpenseRecordDetail>query().lambda()
                .ne(BasicExpenseRecordDetail::getCostType, FeeTypeEnum.penalty)
                .gt(BasicExpenseRecordDetail::getRemainingAmt, Constants.ZERO)
                .eq(BasicExpenseRecordDetail::getStatus, ExpenseStatusEnum.unsettle)
        );

        List <String> contractNoList = new ArrayList<>();
        if(list.size() > 0){
            for(BasicExpenseRecordDetail record : list){
                if(record.getExemptedAmt() == null || (record.getExemptedAmt() != null && !(record.getExemptedAmt().compareTo(BigDecimal.ZERO) == 1) )){
                    if( !contractNoList.contains(record.getContractNo())){
                        contractNoList.add(record.getContractNo());
                    }
                }
            }
        }
        condition.setContractNoList(contractNoList);
        return IResponse.success(basicMainInfoService.getCanCostExemptListByCondition(page, condition));

    }


    @PostMapping("/getCanCostExemptListByContractNo")
    @ApiOperation(value = "获取合同可做豁免费用列表")
    public IResponse getCanCostExemptListByContractNo(@RequestBody BasicCostExemptCondition condition) {
        Page page1 = new Page(condition.getPageNumber(), condition.getPageSize());
        IPage<BasicExpenseRecordDetailVo> list = basicMainInfoService.getCanCostExemptListByContractNo(page1, condition);
        return IResponse.success(list);
    }

    @PostMapping("/getCanCostExempt")
    @ApiOperation(value = "判断是否有可做的费用豁免信息")
    public IResponse getCanCostExempt(@RequestBody BasicCostExemptCondition condition) {
        Page page1 = new Page(1, 1);
        IPage<BasicExpenseRecordDetailVo> list = basicMainInfoService.getCanCostExemptListByContractNo(page1, condition);
        if(list.getTotal() == 0){
            return IResponse.success(false);
        }
        return IResponse.success(true);
    }



    @PostMapping("/costExemptApprovel")
    @ApiOperation(value = "豁免费用流程处理")
    public IResponse costExemptApprovel(@RequestBody BasicExpenseRecordDetailDto condition) {
        return basicExpenseRecordDetailService.costExemptApprovel(condition,this.getBatchDate());
    }


    @PostMapping("/changeExpenseRecordStatus")
    @ApiOperation(value = "改费用详细表状态")
    public IResponse changeExpenseRecordStatus(@RequestBody BasicExpenseRecordDetailDto dto) throws InterruptedException{
       List<Long> expenseDetailIds = dto.getExpenseDetailIds();
        String status = dto.getStatus();
        boolean result = true;
        if(expenseDetailIds.size() > 0 ){
            for (Long id : expenseDetailIds){
                BasicExpenseRecordDetail basicExpenseRecordDetail = basicExpenseRecordDetailService.getById(id);
                if(ExpenseStatusEnum.underReview.toString().equals(status)){
                    basicExpenseRecordDetail.setStatus(ExpenseStatusEnum.underReview);
                }else{
                    basicExpenseRecordDetail.setStatus(ExpenseStatusEnum.unsettle);
                }
                result = basicExpenseRecordDetailService.updateById(basicExpenseRecordDetail);
            }
        }
        return IResponse.success(result);
    }


    @GetMapping("/CostExemptValid/{contractNo}")
    @ApiOperation(value = "费用豁免时当前合同是否在扣款中校验")
    public IResponse valid(@PathVariable String contractNo) {
        return IResponse.success(basicDebitRecordService.isAllowedChangeRecord(contractNo));
    }

}
